package com.deliveryservicesystem.service.impl;

import com.deliveryservicesystem.dataaccess.DatabaseConnection;
import com.deliveryservicesystem.service.GeneralServices;
import java.sql.*;
import oracle.jdbc.OracleTypes;

/**
 * File Name: GeneralServicesImpl.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.service.impl
 * Author: Lance
 * Date: Jun 20, 2012
 * Editor:
 */
public class GeneralServicesImpl implements GeneralServices
{

    @Override
    public ResultSet getTableDataFromProcedure(String procedureSQL) throws SQLException
    {
        DatabaseConnection databaseConnection = DatabaseConnection.getInstance();
        Connection connection = databaseConnection.getConnection();
        CallableStatement prepareCall = connection.prepareCall(procedureSQL);
        prepareCall.registerOutParameter(1, OracleTypes.CURSOR);
        prepareCall.execute();
        ResultSet resultSet = (ResultSet) prepareCall.getObject(1);
        return resultSet;
    }

    @Override
    public ResultSet getTableDataFromSql(String sql) throws SQLException
    {
        DatabaseConnection databaseConnection = DatabaseConnection.getInstance();
        Connection connection = databaseConnection.getConnection();
        Statement createStatement = connection.createStatement();
        ResultSet resultSet = createStatement.executeQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet getRecordFromTable(String columnId, String tableName, String recordId) throws SQLException
    {
        StringBuilder stringBuilder = new StringBuilder("SELECT * FROM ");
        stringBuilder.append(tableName).append(" WHERE ").append(columnId).append(" = ").append(recordId);

        DatabaseConnection databaseConnection = DatabaseConnection.getInstance();
        Connection connection = databaseConnection.getConnection();
        Statement createStatement = connection.createStatement();
        ResultSet resultSet = createStatement.executeQuery(stringBuilder.toString());
        return resultSet;
    }
}
